﻿using System;
using System.ServiceModel.Configuration;
using System.ServiceModel.Channels;
using System.Configuration;

namespace Microsoft.Biztalk.Adapters.Wcf.NullBinding
{
    class NullBindingStandardBindingElement : StandardBindingElement
    {
        public NullBindingStandardBindingElement(string configurationName)
            : base(configurationName)
        {
        }
        public NullBindingStandardBindingElement()
            : this(null)
        {
        }

        [ConfigurationPropertyAttribute("LogMessages", DefaultValue = (object)false)]
        public bool LogMessages { get { return (bool)base["LogMessages"]; } set { base["LogMessages"] = value; } }

        [ConfigurationPropertyAttribute("IsQueued", DefaultValue = (object)false)]
        public bool IsQueued { get { return (bool)base["IsQueued"]; } set { base["IsQueued"] = value; } }

        [ConfigurationPropertyAttribute("IsTransacted", IsRequired = true, DefaultValue = false)]
        public bool IsTransacted { get { return (bool)base["IsTransacted"]; } set { base["IsTransacted"] = value; } }

        [ConfigurationPropertyAttribute("MessageBodySizeInBytes", IsRequired = true, DefaultValue = 1)]
        public int MessageSize { get { return (int)base["MessageBodySizeInBytes"]; } set { base["MessageBodySizeInBytes"] = value; } }

        [ConfigurationPropertyAttribute("NumberOfChannelsAccepted", IsRequired = true, DefaultValue = 1)]
        public int NumberOfThreads { get { return (int)base["NumberOfReceiveThreads"]; } set { base["NumberOfReceiveThreads"] = value; } }

        [ConfigurationPropertyAttribute("ReceiveMessagesPerSecond", IsRequired = true, DefaultValue = (float)1.1)]
        public float MessagesPerSecond { get { return (float)base["ReceiveMessagesPerSecond"]; } set { base["ReceiveMessagesPerSecond"] = value; } }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                ConfigurationPropertyCollection properties = base.Properties;
                properties.Add(new ConfigurationProperty("LogMessages", typeof(bool), false));
                properties.Add(new ConfigurationProperty("IsQueued", typeof(bool),false));
                properties.Add(new ConfigurationProperty("IsTransacted", typeof(bool), false));
                properties.Add(new ConfigurationProperty("ReceiveMessageSizeInBytes", typeof(int), 1));
                properties.Add(new ConfigurationProperty("NumberOfReceiveThreads", typeof(int), 1));
                properties.Add(new ConfigurationProperty("ReceiveMessagesPerSecond", typeof(float), (float)1.0));
                return properties;
            }
        }

        protected override Type BindingElementType
        {
            get { return typeof(NullBinding); }
        }

        protected override void OnApplyConfiguration(Binding binding)
        {
            NullBinding nullBinding = binding as NullBinding;
            if (binding == null)
                throw new ArgumentException("Binding not Nullbinding");

            nullBinding.IsQueued = this.IsQueued;
            nullBinding.LogMessages = this.LogMessages;
            nullBinding.IsTransacted = this.IsTransacted;
            nullBinding.MessagesPerSecond = this.MessagesPerSecond;
            nullBinding.NumberOfThreads = this.NumberOfThreads;
            nullBinding.MessageBody = "";
            for (int i = 0; i < this.MessageSize; i++)
            {
                nullBinding.MessageBody += "*";
            }
        }
    }
}
